<template>
	<view class="feedback">
		<u-form labelPosition="top" :model="formData" :rules="rules" labelWidth="240rpx" ref="form">
			<view class="feedback-title">
				<text>问题和意见</text>
				<view class="feedback-quick" @tap="chooseMsg">快速键入 <u-icon name="arrow-down"></u-icon></view>
			</view>
			<u-form-item label="意见反馈" prop="feedback" required>
				<u-textarea v-model="formData.feedback" placeholder="请输入" border="none" />
			</u-form-item>

			<!-- <view class="feedback-title">
				<text>问题和意见</text>
				<text class="feedback-quick" @tap="chooseMsg">快速键入 <text class="iconfont iconxia"></text></text>
			</view>
			<view class="feedback-body">
				<textarea placeholder="请详细描述您的意见..." v-model="sendDate.suggest" class="feedback-textare" />
			</view> -->

			<!-- <u-form-item label="上传图片" required>
				<u-upload :fileList="fileList1" name="1" width="70" height="70" @afterRead="afterRead"
					@delete="deletePic" multiple :maxCount="4" />
			</u-form-item> -->
			<u-form-item label="联系电话" prop="phone" required>
				<u-input v-model="formData.phone" placeholder="请输入" border="none" type="number" />
			</u-form-item>
		</u-form>

		<view class="bottom-add-btn">
			<u-button text="提交" shape="circle" @click="submit"
				color="linear-gradient( 270deg, #47B37B 0%, #69D19B 100%, #4BA677 0%)"></u-button>
		</view>
	</view>
</template>

<script>
	const train = require("@/api/train/index.js");
	export default {
		data() {
			return {
				formData: {
					feedback: ''
				},
				rules: {
					feedback: {
						required: true,
						message: '请输入意见反馈',
						trigger: ['change'],
					},
					phone: {
						required: true,
						message: '请输入联系电话',
						trigger: ['change'],
					},
				},
				fileList: [],
				fileList1: [],
				url_phone: '',
				resulturl: '',
				url_list12: [],
				msgContents: [
					'小程序卡',
					'前台态度差',
					'小程序繁琐',
				],
				sendDate: {
					suggest: '',
				},
			}
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			// this.$refs.form1.setRules(this.rules)
		},
		methods: {
			// 快速输入
			chooseMsg() {
				uni.showActionSheet({
					itemList: this.msgContents,
					success: res => {
						this.formData.feedback = this.msgContents[res.tapIndex];
					}
				});
			},
			// 删除图片
			deletePic(event) {
				this.url_list12.splice(event.index, 1)
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				this.resulturl = url
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://oss.szsportscenter.cn/api/upload/uploadOneImage', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							// user: 'test',
							project_id: getApp().globalData.projectId,
							app_id: this.$https.weixinAppId,
						},
						header: {
							'content-type': 'multipart/form-data'
						},
						success: (res) => {
							let result = JSON.parse(res.data);
							this.url_phone = result.data.url
							this.url_list12.push(result.data)
							setTimeout(() => {
								resolve(this.url_phone)
							}, 1000)
						}
					});
				})
			},
			// deletePic(event) {
			// 	this.fileList.splice(event.index, 1);
			// },
			// async afterRead(event) {
			// 	this.fileList.concat(event.file);
			// },

			async submit() {
				try {
					const valid = await this.$refs.form.validate();
					if (valid) {
						let postData = {
							// type: 0,
							app_id: this.$https.weixinAppId,
							project_id: getApp().globalData.projectId,
							open_id: uni.getStorageSync("openid"),
							content: this.formData.feedback,
							pic: this.url_phone,
							phone: this.formData.phone,
						}
						let res = await train.Suggestion(postData)
						if (res.code == 1) {
							uni.navigateTo({
								url: '/pages/personal/personal'
							})
						}
					}
				} catch (e) {

				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.bottom-add-btn {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 20rpx;
		padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
		background-color: #fff;
		z-index: 1;
	}

	.feedback {
		min-height: 100vh;
		background-color: #F2F7F5;
		padding: 30rpx;

		/deep/.u-form {
			background-color: #fff;
			border-radius: 10rpx;
			margin-bottom: 20rpx;
			padding: 20rpx 0;

			.u-form-item {
				padding: 0 30rpx;
			}
		}
	}

	.uni-timeline {
		padding: 20rpx;
		background-color: #fff;
	}

	.feedback-title {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		padding: 11px;
		margin-top: 11px;
		font-size: 16px;
		background-color: #fff;
	}

	.feedback-star-view.feedback-title {
		justify-content: flex-start;
		margin: 0;
	}

	.feedback-quick {
		position: relative;
		// padding-right: 40upx;
		display: flex;
		flex-direction: row;

		.iconfont {
			font-size: 24rpx;
		}
	}

	.feedback-body {
		background: #fff;
		padding: 5px 11px;

		.gender-item {
			margin-right: 20upx;

			.gender-item-text {
				padding-left: 10upx;
			}

			radio .wx-radio-input.wx-radio-input-checked {
				background: #fff;
				border-color: #606266;
			}
		}
	}

	.feedback-textare {
		height: 1200upx;
		font-size: 34upx;
		line-height: 50upx;
		width: 100%;
		box-sizing: border-box;
		padding: 20upx 30upx 0;
	}

	.footer {
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 98;
		width: 100%;
		background-color: #fff;
		color: #606266;
		box-shadow: 0 -1px 5px rgba(0, 0, 0, 0.1);
		display: flex;
		justify-content: flex-end;
		align-items: center;
		padding: 8px 11px;

		.action-btn {
			font-size: 24rpx;
			margin: 0 0 0 15upx;
			padding: 0 20rpx;
			text-align: center;
			height: 60upx;
			line-height: 60upx;
			// background-color: #F37B1D;
			// color: #fff;
		}
	}
</style>